package com.study.talk.module.controller;

import com.study.talk.common.resullt.FwResult;
import com.study.talk.common.util.SecurityUtil;
import com.study.talk.module.entity.GroupInfo;
import com.study.talk.module.entity.GroupUserInfo;
import com.study.talk.module.entity.vo.GroupUserInfoVo;
import com.study.talk.module.service.GroupInfoService;
import com.study.talk.module.service.GroupUserInfoService;
import com.study.talk.module.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author thor
 * @since 2023-12-18
 */
@RestController
@RequestMapping("/groupUserInfo")
public class GroupUserInfoController {


    @Resource
    private GroupUserInfoService groupUserInfoService;

    @Resource
    private GroupInfoService groupInfoService;

    @Resource
    private UserService userService;

    @ApiOperation("获取该群的所有成员")
    @GetMapping("/get/user/list/{groupId}")
    public FwResult<List<GroupUserInfo>> getUserInfoList(@PathVariable("groupId")Integer groupId){
        return FwResult.ok(groupInfoService.getMembers(groupId));
    }

    @ApiOperation("查询该群是否有该成员")
    @PostMapping("/query/has/member")
    public FwResult<Boolean> queryHasMember(@RequestBody GroupUserInfoVo groupUserInfoVo){
        return FwResult.ok(groupUserInfoService.queryHasMember(groupUserInfoVo.getGroupId(),groupUserInfoVo.getUserId()));
    }

    @ApiOperation("退出群聊")
    @DeleteMapping("/exit/{group_id}")
    public FwResult<Boolean> exitGroup(@PathVariable("group_id") Integer groupId){
        Integer userId = SecurityUtil.getUser().getId();
        groupUserInfoService.deleteMember(userId,groupId);
        //TODO 通知群主成员退出
        return FwResult.ok(true);
    }

    @ApiOperation("剔除群聊")
    @DeleteMapping("/exit/{group_id}/{user_id}")
    public FwResult<Boolean> deleteMember(@PathVariable("group_id") Integer groupId,@PathVariable("user_id") Integer userId){
        groupUserInfoService.deleteMember(userId,groupId);
        return FwResult.ok(true);
    }
}
